package com.ocom.webapi.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WebApiServiceUrl;
import com.ocom.common.dto.Department;
import com.ocom.common.request.ComIdRequest;
import com.ocom.common.request.company.*;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.security.authentication.YoCiUser;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.feign.CompanyClientService;
import com.ocom.webapi.feign.HumanClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.awt.font.TextHitInfo;

@RestController
public class FlowConfigController {
    @Autowired
    private CompanyClientService companyClientService;

    @Autowired
    private HumanClientService humanClientService;


    @PostMapping(WebApiServiceUrl.URL_FlowConfigFlowList)
    public  Result flowList(@RequestBody GetFlowListByComRequest getFlowListByComRequest){
        return  this.companyClientService.getFlowListByCom(getFlowListByComRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigAddFlow)
    public Result addFlow(@RequestBody NewComFlowRequest newComFlowRequest){
        return this.companyClientService.newComFlow(newComFlowRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigModifyFlow)
    public Result modifyFlow(@RequestBody UpdateComFlowRequest updateComFlowRequest){
        return this.companyClientService.updateComFlow(updateComFlowRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigDeleteFlow)
    public Result deleteFlow(@RequestBody RemoveComFlowRequest removeComFlowRequest){
        return this.companyClientService.removeComFlow(removeComFlowRequest);
    }


    @PostMapping(WebApiServiceUrl.URL_FlowConfigApproverList)
    public Result approverList(@RequestBody GetApproverListByFlowRequest getApproverListByFlowRequest){
        return this.companyClientService.getApproverListByFlow(getApproverListByFlowRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigAddApprover)
    public Result addApprover(@RequestBody NewFlowApproverRequest newFlowApproverRequest){
        return this.companyClientService.newFlowApprover(newFlowApproverRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigModifyApprover)
    public Result modifyApprover(@RequestBody UpdateFlowApproveRequest updateFlowApproveRequest){
        return this.companyClientService.updateFlowApprover(updateFlowApproveRequest);
    }


    @PostMapping(WebApiServiceUrl.URL_FlowConfigDeleteApprover)
    public Result deleteApprover(@RequestBody RemoveFlowApproverRequest removeFlowApproverRequest){
        return this.companyClientService.removeFlowApprover(removeFlowApproverRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigPushList)
    public Result pushList(@RequestBody GetPushListByFlowRequest getPushListByFlowRequest){
        return  this.companyClientService.getPushListByFlow(getPushListByFlowRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigAddPush)
    public Result addPush(@RequestBody NewFlowPushRequest newFlowPushRequest){
        return  this.companyClientService.newFlowPush(newFlowPushRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigModifyPush)
    public Result modifyPush(@RequestBody UpdateFlowPushRequest updateFlowPushRequest){
        return this.companyClientService.updateFlowPush(updateFlowPushRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowConfigDeletePush)
    public Result deletePush(@RequestBody RemoveFlowPushRequest removeFlowPushRequest){
        return this.companyClientService.removeFlowPush(removeFlowPushRequest);
    }

    @PostMapping(WebApiServiceUrl.URL_FlowInstanceList)
    public Result FlowInstanceList(@RequestBody FlowInstanceListRequest flowInstanceListRequest){
        YoCiUser yoCiUser = SecurityUtils.getYoCiUser();
        flowInstanceListRequest.setComId(yoCiUser.getComId());
        GetFlowInstanceListRequest getFlowInstanceListRequest = Convert.convert(GetFlowInstanceListRequest.class,flowInstanceListRequest);


        Department department=new Department();
        department.setComId(flowInstanceListRequest.getComId());
        if(ObjectUtil.isNotEmpty(flowInstanceListRequest.getDeptId())){
            department.setDeptId(flowInstanceListRequest.getDeptId());
        }

        department.setShowLevel(0);

        Result bootparentDeptId = this.humanClientService.getDeptIds(department);
        if (bootparentDeptId.getCode()==200){
            JSONObject bootparentDeptIdObject= JSON.parseObject(JSON.toJSONString(bootparentDeptId.getData()));
            String deptIds="";
            if (bootparentDeptIdObject.getString("deptIds").equals("")){
                deptIds= String.valueOf(flowInstanceListRequest.getDeptId());
            }else {
                deptIds=flowInstanceListRequest.getDeptId()+","+bootparentDeptIdObject.getString("deptIds");
            }
            getFlowInstanceListRequest.setDeptIds(deptIds);
        }else {
            if(ObjectUtil.isNotEmpty(flowInstanceListRequest.getDeptId())){
                getFlowInstanceListRequest.setDeptIds(String.valueOf(flowInstanceListRequest.getDeptId()));
            }
        }
        return this.companyClientService.getFlowInstanceList(getFlowInstanceListRequest);
    }
}
